﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Sprites;
using UnityEngine.UI;
namespace TK.UI.Common
{
    /// <summary>
    /// 
    /// </summary>
    public class CircleImage : Image
    {
        public int TotalCount = 100;

        public int RealTotalCount;

        public float CoolPercent = 1f;

        public Color32 MaskColor = new Color32(255, 255, 255, 100);

        //protected List<UIVertex> vertices = new List<UIVertex>();
        float rectWidth;
        float rectHeight;
        Vector4 uv;
        float uvHeight;
        float uvWidth;
        Vector2 uvCenter;
        Vector2 rate;
        float radian;
        float radius;

        protected override void Awake()
        {
            //TotalCount = 100;
            RealTotalCount = (int)(TotalCount * CoolPercent);
            // 得到 transform 的宽高
            rectWidth = rectTransform.rect.width;
            rectHeight = rectTransform.rect.height;
            // 得到uv对象
            uv = overrideSprite != null ? DataUtility.GetOuterUV(overrideSprite) : Vector4.zero;
            // 得到uv的宽高
            uvWidth = uv.z - uv.x;
            uvHeight = uv.w - uv.y;
            // 得到uv的中心
            uvCenter = new Vector2(uvWidth * 0.5f, uvHeight * 0.5f);
            // uv和transform的比例
            rate = new Vector2(uvWidth / rectWidth, uvHeight / rectHeight);

            // 算出每份的弧度
            radian = (2 * Mathf.PI) / TotalCount;
            // 圆形半径
            radius = rectWidth * 0.5f;
        }

        protected override void OnPopulateMesh(VertexHelper vh)
        {
            // 清除之前的图片
            vh.Clear();
            //// 得到 transform 的宽高
            //float width = rectTransform.rect.width;
            //float height = rectTransform.rect.height;
            //// 得到uv对象
            //Vector4 uv = overrideSprite != null ? DataUtility.GetOuterUV(overrideSprite) : Vector4.zero;
            //// 得到uv的宽高
            //float uvHeight = uv.z - uv.x;
            //float uvWidth = uv.w - uv.y;
            //// 得到uv的中心
            //Vector2 uvCenter = new Vector2(uvWidth * 0.5f, uvHeight * 0.5f);
            //// uv和transform的比例
            //Vector2 rate = new Vector2(uvWidth/width,uvHeight/height);

            //// 算出每份的弧度
            //float radian = (2 * Mathf.PI) / totalCount;
            //// 圆形半径
            //float radius = width * 0.5f;

            Vector2 tempPos = Vector2.zero;

            // 计算中心点uv
            UIVertex origin = new UIVertex();
            origin.color = color;
            origin.position = tempPos;
            origin.uv0 = new Vector2(tempPos.x * rate.x + uvCenter.x, tempPos.y * rate.y + uvCenter.y);
            vh.AddVert(origin);
            //vertices.Add(origin);

            // 当前弧度
            float curRadian = 0;
            // 每个三角面片
            int cut = RealTotalCount + 1;
            for (int i = 0; i < TotalCount + 1; i++)
            {
                // 计算出x和y
                float x = Mathf.Cos(curRadian) * radius;
                float y = Mathf.Sin(curRadian) * radius;
                curRadian += radian;

                tempPos = new Vector2(x, y);
                // 算出每个点
                UIVertex temp = new UIVertex();
                if (i < cut)
                {
                    temp.color = color;
                }
                else
                {
                    temp.color = MaskColor;
                }
                temp.position = tempPos;
                temp.uv0 = new Vector2(tempPos.x * rate.x + uvCenter.x, tempPos.y * rate.y + uvCenter.y);
                vh.AddVert(temp);
                //vertices.Add(temp);
            }

            for (int i = 1; i <= TotalCount; i++)
            {
                vh.AddTriangle(i, 0, i + 1);
            }
        }

        void Update()
        {
            CoolPercent -= Time.deltaTime * 0.2f;
            if (CoolPercent <= 0)
            {
                CoolPercent = 1;
            }
        }
    }
}

